const app = getApp()
const { comNameApi } = require('../../../api/services/searchCompany/index')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    comName: '',
    comList: [],
    params: { pageSize: 15, pageNum: 1, comName: '' },
    isLoading: false, //节流状态值
    dataMore: true,
  },
  // 搜索
  async onSearch(event) {
    this.setData({ comName: event.detail })
    let params = { comName: event.detail }

    const res = await comNameApi(params)

    console.log(res)
    this.setData({ comList: res.rows })
  },

  handleItem(event) {
    let { item } = event.currentTarget.dataset

    let itemStr = JSON.stringify(item)
    wx.navigateTo({
      url: '/pages/admin/yhpcAdd/yhpcAdd?item=' + itemStr,
    })
  },

  async getComList() {
    this.setData({ isLoading: true })
    if (!this.data.dataMore) return
    const res = await comNameApi(this.data.params)

    if (res.code != 200) {
      return app.toast(res.msg)
    }
    console.log(res)
    const newList = [...this.data.comList, ...res.rows]
    this.setData({
      comList: newList,
      isLoading: false,
      dataMore: newList.length < res.total,
    })
  },
  //取消
  onCancel() {
    wx.navigateBack({
      delta: 1,
    })
  },

  onLoad: function (options) {
    this.getComList()
  },

  onReachBottom: function () {
    if (this.data.isLoading) return
    this.data.params.pageNum++
    this.getComList()
  },
})
